//
//  ASInfoCell.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/2/24.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASInfoCell.h"

@implementation ASInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        CGFloat fontSize = 16;
        CGFloat padding = 17;
        CGFloat bgWidth = [[UIScreen mainScreen] bounds].size.width;
        
        // leftText
        _leftText = [[UILabel alloc] initWithFrame:CGRectZero];
        _leftText.frame = CGRectMake(padding, 5, 90, 40);
        [_leftText setBackgroundColor:[UIColor clearColor]];
        [_leftText setTextColor:RGB(51, 51, 51)];
        [_leftText setFont:[UIFont systemFontOfSize:fontSize]];
        [self.contentView addSubview:_leftText];
        
        // rightText
        _rightText = [[UITextField alloc] initWithFrame:CGRectZero];
        _rightText.frame = CGRectMake(_leftText.frame.origin.x + _leftText.frame.size.width + 5,
                                      (50 - 38) / 2,
                                      bgWidth - (_leftText.frame.origin.x + _leftText.frame.size.width + 5) - 15,
                                      38);
        _rightText.clearButtonMode = UITextFieldViewModeWhileEditing;
        _rightText.textColor = [UIColor blackColor];
        _rightText.returnKeyType = UIReturnKeyDone;
        [_rightText setBackgroundColor:[UIColor clearColor]];
        _rightText.placeholder = @"";
        _rightText.delegate = self;
        _rightText.font = [UIFont systemFontOfSize:18.0f];
        //_rightText.borderStyle = UITextBorderStyleRoundedRect;
        _rightText.borderStyle = UITextBorderStyleNone;
        _rightText.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_rightText];
        
        //portraitImage
        _portraitImage = [[UIImageView alloc] initWithFrame:CGRectMake(bgWidth - 80, 5, 40, 40)];
        _portraitImage.backgroundColor = [UIColor clearColor];
        _portraitImage.image = [UIImage imageNamed:@"portrait_3"];
        [self.contentView addSubview:_portraitImage];
        
        // topLine
        _topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bgWidth, 1)];
        [_topLine setBackgroundColor:RGB(221.0, 221.0, 221.0)];
        [self.contentView addSubview:_topLine];
        
        // bottomLine
        _bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50 - 1, bgWidth, 1)];
        [_bottomLine setBackgroundColor:RGB(221.0, 221.0, 221.0)];
        [self.contentView addSubview:_bottomLine];
        
        // separateLine
        UIImageView* separateLine = [[UIImageView alloc] initWithFrame:CGRectMake(padding,
                                                                                  50 - 1,
                                                                                  bgWidth - padding * 2,
                                                                                  1)];
        [separateLine setBackgroundColor:[UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0]];
        [self.contentView addSubview:separateLine];
        
        // 为宿舍专门使用的，初始化的时候 默认设置为hidden=YES
        _dormitoryB = [[UITextField alloc] initWithFrame:CGRectZero];
        _dormitoryB.frame = CGRectMake(bgWidth - padding - 75, (50 - 38) / 2, 75, 38);
        _dormitoryB.clearButtonMode = UITextFieldViewModeWhileEditing;
        _dormitoryB.textColor = [UIColor blackColor];
        _dormitoryB.returnKeyType = UIReturnKeyDone;
        [_dormitoryB setBackgroundColor:[UIColor clearColor]];
        _dormitoryB.placeholder = @"";
        _dormitoryB.delegate = self;
        _dormitoryB.font = [UIFont systemFontOfSize:18.0f];
        _dormitoryB.borderStyle = UITextBorderStyleNone;
        _dormitoryB.textAlignment = NSTextAlignmentRight;
        _dormitoryB.hidden = YES;
        _dormitoryB.borderStyle = UITextBorderStyleRoundedRect;
        [self.contentView addSubview:_dormitoryB];
        
        _dorSepa = [[UILabel alloc] initWithFrame:CGRectZero];
        _dorSepa.frame = CGRectMake(_dormitoryB.frame.origin.x - 30, 5, 20, 40);
        [_dorSepa setBackgroundColor:[UIColor clearColor]];
        [_dorSepa setTextColor:RGB(51, 51, 51)];
        [_dorSepa setFont:[UIFont systemFontOfSize:fontSize]];
        _dorSepa.text = @"栋";
        _dorSepa.hidden = YES;
        [self.contentView addSubview:_dorSepa];
        
        _dormitoryA = [[UITextField alloc] initWithFrame:CGRectZero];
        _dormitoryA.frame = CGRectMake(_dorSepa.frame.origin.x - 70, (50 - 38) / 2, 60, 38);
        _dormitoryA.clearButtonMode = UITextFieldViewModeWhileEditing;
        _dormitoryA.textColor = [UIColor blackColor];
        _dormitoryA.returnKeyType = UIReturnKeyDone;
        [_dormitoryA setBackgroundColor:[UIColor clearColor]];
        _dormitoryA.placeholder = @"";
        _dormitoryA.delegate = self;
        _dormitoryA.font = [UIFont systemFontOfSize:18.0f];
        _dormitoryA.borderStyle = UITextBorderStyleNone;
        _dormitoryA.textAlignment = NSTextAlignmentRight;
        _dormitoryA.hidden = YES;
        _dormitoryA.borderStyle = UITextBorderStyleRoundedRect;
        [self.contentView addSubview:_dormitoryA];
    }
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    return self;
}

- (void)updateCellStatus:(NSString *)leftText
{
    _leftText.text = leftText;
    _rightText.placeholder = @"请输入...";
    
    //宿舍相关的置为hidden
    _dormitoryB.hidden = YES;
    _dorSepa.hidden = YES;
    _dormitoryA.hidden = YES;
    
    // _rightText
    if ([leftText isEqualToString:@"姓名"])
    {
        _topLine.hidden = NO;
        _bottomLine.hidden = YES;
        _rightText.hidden = NO;
        _rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.userName;
    }
    else if ([leftText isEqualToString:@"联系方式"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = NO;
        _rightText.hidden = NO;
        _portraitImage.hidden = YES;
        _rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.userMobile;
    }
    else if ([leftText isEqualToString:@"班级"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        _rightText.hidden = NO;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        _rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.className;
    }
    else if ([leftText isEqualToString:@"宿舍"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        //_rightText.hidden = NO;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        //_rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.dormitory;
        
        _rightText.hidden = YES;
        _rightText.placeholder = @"";
        _dormitoryB.hidden = NO;
        _dorSepa.hidden = NO;
        _dormitoryA.hidden = NO;
        
        //解析宿舍字段 25#428
        NSString *dor = [ASAppDelegate sharedAppDelegate].userInfo.dormitory;
        NSRange range = [dor rangeOfString:@"#"];
        if (range.length > 0)
        {
            _dormitoryA.text = [dor substringToIndex:range.location];
            _dormitoryB.text = [dor substringFromIndex:(range.location + 1)];
        }
    }
    else if ([leftText isEqualToString:@"政治面貌"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        _rightText.hidden = NO;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        NSString* strNum = [ASAppDelegate sharedAppDelegate].userInfo.political;
        NSMutableArray *arr = [ASAppDelegate sharedAppDelegate].politicalInfoArr;
        for (NSInteger i = 0; i < [arr count]; i++)
        {
            ASPoliticalInfo *info = [arr objectAtIndex:i];
            if ([strNum integerValue] == info.political)
            {
                _rightText.text = info.politicalName;
            }
        }
    }
    else if ([leftText isEqualToString:@"邮箱"])
    {
        _rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.userEmail;
    }
    else if ([leftText isEqualToString:@"性别"])
    {
        NSString *sex = [ASAppDelegate sharedAppDelegate].userInfo.sex;
        if ([sex isEqualToString:@"F"])
        {
            _rightText.text = @"女";
        }
        else
        {
            _rightText.text = @"男";
        }
    }
    else if ([leftText isEqualToString:@"出生年月"])
    {
        _rightText.text = [ASAppDelegate sharedAppDelegate].userInfo.userBirth;
    }
    
    if ([leftText isEqualToString:@"我的头像"])
    {
        _topLine.hidden = NO;
        _bottomLine.hidden = YES;
        _rightText.hidden = YES;
        _portraitImage.hidden = NO;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    else if ([leftText isEqualToString:@"出生年月"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = NO;
        _rightText.hidden = NO;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    else if ([leftText isEqualToString:@"获奖成果"])
    {
        _topLine.hidden = NO;
        _bottomLine.hidden = NO;
        _rightText.hidden = YES;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        _rightText.hidden = NO;
        _portraitImage.hidden = YES;
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    
    _portraitImage.hidden = YES;
}


/**
 * 实习单位专用
 @param leftText 左侧标题
 */
- (void)updatePracticeUnitCellStatus:(NSString *)leftText
{
    _leftText.text = leftText;
    _rightText.placeholder = @"请输入...";
    
    //宿舍相关的置为hidden
    _dormitoryB.hidden = YES;
    _dorSepa.hidden = YES;
    _dormitoryA.hidden = YES;
    _portraitImage.hidden = YES;
    self.accessoryType = UITableViewCellAccessoryNone;
    
    // _rightText @"账户编号",@"账户名称",@"单位性质",@"账户地址"
    if ([leftText isEqualToString:@"账户编号"])
    {
        _topLine.hidden = NO;
        _bottomLine.hidden = YES;
        
        _rightText.text = [ASAppDelegate sharedAppDelegate].pracUnitUserInfo.accountCode;
    }
    else if ([leftText isEqualToString:@"账户名称"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        
        _rightText.text = [ASAppDelegate sharedAppDelegate].pracUnitUserInfo.accountName;
    }
    else if ([leftText isEqualToString:@"单位性质"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
        
        _rightText.text = [ASAppDelegate sharedAppDelegate].pracUnitUserInfo.accountProperty;
    }
    else if ([leftText isEqualToString:@"账户地址"])
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = NO;
        
        _rightText.text = [ASAppDelegate sharedAppDelegate].pracUnitUserInfo.accountAddress;
    }
    else
    {
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([_leftText.text isEqualToString:@"政治面貌"])
    {
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectPoliticalInfo:)])
        {
            [self.delegate selectPoliticalInfo:nil];
        }
        
        return NO;
    }
    return YES;
}



@end




